﻿namespace LoggingBot
{
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.IO;
    using System.Net;
    using System.Net.Sockets;
    using System.Text;
    using Exiled.API.Enums;
    using Exiled.API.Features;
    using MEC;
    using RgCore.Modules;

    public class LoggingPlugin : RgPlugin
    {
        public override string Name => "事件记录机器人";

        public override string Id => "logging_bot";

        public override PluginPriority Priority => PluginPriority.Lowest;

        public override void OnDisabled()
        {
            base.OnDisabled();
            EventHandlers.UnRegisterEvents();
        }

        public override void OnEnabled()
        {
            base.OnEnabled();
            Logging.Awake();
        }
    }

    public static class Logging
    {
        public static bool Offline = false;
        public static List<string> CachedMessage = new List<string>();
        private static IPAddress ipAddress = IPAddress.Parse("127.0.0.1");
        private static IPEndPoint remoteEP = new IPEndPoint(ipAddress, 18000);

        private static string ServerName
        {
            get
            {
                string[] array = Server.Name.Split(new char[] { '◈' });
                return array[1].Replace("</color>", string.Empty).Replace("<color=#FF0000>", string.Empty);
            }
        }

        public static void SendMessage(string content)
        {
            if (Offline)
            {
                return;
            }
            if (!Sendmessage(content))
            {
                Process.Start(Path.Combine(Paths.Plugins, "LoggingBot", "CdcpLoggingBot.exe"));
                Timing.CallDelayed(0.5f, () =>
                {
                    if (!Sendmessage(content))
                    {
                        Offline = true;
                    }
                });
            }
        }

        public static void SendCachedMessage(string title)
        {
            string message = string.Empty;
            foreach (string msg in CachedMessage)
            {
                message += "#" + msg;
            }

            SendMessage($"[{title}]" + message);
            CachedMessage.Clear();
        }

        public static void Awake()
        {
            Timing.CallDelayed(2f, () =>
            {
                SendMessage("服务器已连接");
                EventHandlers.RegisterEvents();
            });
        }

        private static bool Sendmessage(string content)
        {
            Socket sender = new Socket(
                ipAddress.AddressFamily,
                SocketType.Stream,
                ProtocolType.Tcp);
            try
            {
                sender.Connect(remoteEP);
                string text = ServerName + " " + content;
                byte[] msg = Encoding.UTF8.GetBytes(text);
                sender.Send(msg);
                sender.Shutdown(SocketShutdown.Both);
                sender.Close();
            }
            catch
            {
                return false;
            }

            return true;
        }
    }
}